package com.zhike.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhike.domain.DwsEnergySocWindow;
import com.zhike.service.DwsEnergySocWindowService;
import com.zhike.mapper.DwsEnergySocWindowMapper;
import com.zhike.utils.BarUtils;
import com.zhike.utils.Result;
import com.zhike.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【dws_energy_soc_window(车辆能耗信息表)】的数据库操作Service实现
* @createDate 2024-05-01 21:05:55
*/
@Service
public class DwsEnergySocWindowServiceImpl extends ServiceImpl<DwsEnergySocWindowMapper, DwsEnergySocWindow>
    implements DwsEnergySocWindowService{
    @Autowired
    private DwsEnergySocWindowMapper dwsEnergySocWindowMapper;

    @Override
    public String getSocWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergySocWindowMapper.getSocWeek(date);
            Map data = BarUtils.getDetailSingle(avgSpeedToday,"vin","soc","百公里soc消耗");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getSocTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergySocWindowMapper.getSocTypeWeek(date);
            Map data = BarUtils.getDetailSingle(avgSpeedToday,"car_type","soc","百公里soc消耗");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getSocBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergySocWindowMapper.getSocBrandWeek(date);
            Map data = BarUtils.getDetailSingle(avgSpeedToday,"brand","soc","百公里soc消耗");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getSocNearWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergySocWindowMapper.getSocNearWeek(date);
            Map data = BarUtils.getDetailSingle(avgSpeedToday,"vin","soc","最近百公里soc消耗");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getSocNearTypeWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergySocWindowMapper.getSocNearTypeWeek(date);
            Map data = BarUtils.getDetailSingle(avgSpeedToday,"car_type","soc","最近百公里soc消耗");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }

    @Override
    public String getSocNearBrandWeek(String filter) {
        String date = BarUtils.getDate(filter);
        try {
            List<Map> avgSpeedToday = dwsEnergySocWindowMapper.getSocNearBrandWeek(date);
            Map data = BarUtils.getDetailSingle(avgSpeedToday,"brand","soc","最近百公里soc消耗");
            return Result.ok(JSON.toJSONString(data)).toString();
        } catch (NumberFormatException e) {
            return Result.build(null, ResultCodeEnum.FAIL).toString();
        }
    }
}




